package com.hhs.storage.controller;


import com.hhs.storage.bean.BasePermission;
import com.hhs.storage.bean.BasePermissionUser;
import com.hhs.storage.bean.InputParam;
import com.hhs.storage.bean.ResponseInfo;
import com.hhs.storage.service.BasePermissionService;
import com.hhs.storage.util.SysUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hhs
 * @since 2023-01-09
 */
@RestController
@RequestMapping("/permission")
public class BasePermissionController {
    @Resource
    HttpServletRequest request;

    @Resource
    BasePermissionService basePermissionService;

    @RequestMapping(value = "getAll")
    public Map<String, Object> getAll(InputParam inputParam) {

        return basePermissionService.getAll(inputParam);
    }
    //根据用户id，获取用户的权限点
    @RequestMapping(value = "getByUserId")
    public Map<String, Object> getByUserId(String userId) {
        if(StringUtils.isEmpty(userId)){
            //获取当前用户的权限
            userId= SysUtil.getLoginUserId(request);
        }//其他情况，从外部传递用户id进来，查询该用户的权限
        List<BasePermission> list = basePermissionService.getByUserId(userId);
        String temp=",,";
        for (BasePermission basePermission : list) {
            temp+=basePermission.getId()+',';
        }
        return  ResponseInfo.success(temp);
    }

    @RequestMapping(value = "edit")
    public Map<String, Object> edit(boolean status,String perId,String userId) {
        if(StringUtils.isEmpty(userId) ||StringUtils.isEmpty(perId) ){
            return  ResponseInfo.error("参数错误");
        }

        basePermissionService.configData( status, perId, userId);
        return  ResponseInfo.success(null);
    }
}

